import 'package:shared_preferences/shared_preferences.dart';

class TokenManager {
  static TokenManager? _tokenmanager;
  String _token = '';
  final String KEY_TOKEN = 'token';
  SharedPreferences? prefs;
  TokenManager._();

  initSp() async {
    prefs = await SharedPreferences.getInstance();
    String? tokenStr = prefs!.getString(KEY_TOKEN);
    if (tokenStr == null || tokenStr.isEmpty) {
      _token = '';
    } else {
      _token = tokenStr;
    }
  }

  factory TokenManager() {
    if (_tokenmanager == null) {
      _tokenmanager = TokenManager._();
      return _tokenmanager!;
    } else {
      return _tokenmanager!;
    }
  }

  String getToken() {
    return _token;
  }

  Future<void> setToken(String token) async {
    await prefs?.setString(KEY_TOKEN, token);
    _token = token;
  }
}
